package org.linlinjava.litemall.admin.controller;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.service.FeedbackService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.dao.LitemallFeedbackMapper;
import org.linlinjava.litemall.db.domain.LitemallFeedback;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallFeedbackService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 管理反馈控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@CrossOrigin
@Api(tags = "管理反馈API")
@RestController
@RequestMapping("/admin/feedback")
@Validated
public class AdminFeedbackController {

    /**
     * litemall反馈服务
     */
    @Autowired
    private LitemallFeedbackService litemallFeedbackService;
    /**
     * 反馈服务
     */
    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private LitemallUserService litemallUserService;

    /**
     * litemall反馈映射器
     */
    @Autowired
    private LitemallFeedbackMapper litemallFeedbackMapper;

    /**
     * 反馈列表
     *
     * @param userId   用户id
     * @param username 用户名
     * @param page     页面
     * @param limit    限制
     * @param sort     排序
     * @param order    订单
     * @return {@link Object}
     */
    @ApiOperation(value = "反馈列表")
    @GetMapping("/list")
    @LogAnno
    public Object list(Integer userId, String username,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallFeedback> litemallFeedbackList = litemallFeedbackService.querySelective(userId, username, page, limit, sort, order);
        litemallFeedbackList.stream().forEach(litemallFeedback -> {
            LitemallUser user = litemallUserService.findById(litemallFeedback.getUserId());
            litemallFeedback.setNickname(ObjectUtil.isNotNull(user)?user.getNickname():"用户注销");
        });
        //将查出来的变成旧反馈 异步执行
        CompletableFuture.runAsync(()->
                litemallFeedbackList.stream().forEach(feedback -> {
                feedback.setNewFeedback(0);
                litemallFeedbackService.updateById(feedback);
            })
        );
        return ResponseUtil.okList(litemallFeedbackList);
    }

    /**
     * 新的反馈数 (侧边栏显示的数量)
     *
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation(value = "新的反馈数")
    @GetMapping(value = "newFeedbacks")
    @LogAnno
    public ResponseUtil.Response newFeedbacks(){
        Integer number = litemallFeedbackMapper.selectList(Wrappers.lambdaQuery(LitemallFeedback.class)
                .ge(LitemallFeedback::getNewFeedback, 1)
                .orderByDesc(LitemallFeedback::getAddTime)).size();
        return ResponseUtil.ok(number);
    }

    /**
     * 回复
     *
     * @param feedback 反馈
     * @return {@link Object}
     */
    @PostMapping("/reply")
    @LogAnno
    public Object reply(@RequestBody LitemallFeedback feedback) {
        return feedbackService.replay(feedback);
    }

    /**
     * 忽略
     *
     * @param feedback 反馈
     * @return {@link Object}
     */
    @PostMapping("/ignore")
    @LogAnno
    public Object ignore(@RequestBody LitemallFeedback feedback) {
        return feedbackService.ignore(feedback);
    }
}
